﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YiLe.BLL.Service;
using YiLe.Model.Domain;
using YiLe.View.Table;
using YiLe.Model.Util;

namespace YiLe.View.Login
{
    public partial class LoginForm : Form
    {
        private Spring.Context.IApplicationContext ctx;
        private IUserService userService;
        private IMenuService menuService;

        public LoginForm()
        {
            InitializeComponent();
        }

        public LoginForm(Spring.Context.IApplicationContext context)
        {
            InitializeComponent();
            userService = context["UserService"] as IUserService;
            menuService = context["MenuService"] as IMenuService;
            ctx = context;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string loginname = tbLoginName.Text.ToString();
            string loginpwd = tbLoginPWD.Text.ToString();
            if (userService.Login(loginname, loginpwd))
            {
                //此处通过用户名来查找菜单List，user->role->permission->menu，已完成
                YiLe.Model.Domain.User user = userService.Get(loginname, loginpwd);
                IDictionary<YiLe.Model.Domain.Menu, IList<YiLe.Model.Domain.Menu>> menus = menuService.GetMenus(user.LoginName);
                MainForm mainForm = new MainForm(ctx, menus);
                mainForm.IsMdiContainer = true;//设置为MDI窗体
                mainForm.WindowState = FormWindowState.Maximized;
                mainForm.Show();

                //初始化一个桌面Form
                /*
                TableListForm tlForm = new TableListForm(ctx);
                tlForm.Width = Const.LIST_FORM_WIDTH;
                tlForm.Height = Const.LIST_FORM_HEIGHT;
                tlForm.MdiParent = mainForm;
                tlForm.WindowState = FormWindowState.Maximized;
                tlForm.StartPosition = FormStartPosition.CenterScreen;
                //tlForm.MaximizeBox = false;
                //tlForm.MinimizeBox = false;
                //tlForm.ControlBox = false;
                tlForm.Show();
                */
                this.Hide();
            }
            else 
            {
                LoginFail();
            }
        }

        /// <summary>
        /// 是否是一次菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        private static bool IsMainMenu(YiLe.Model.Domain.Menu menu)
        {
            return menu.ParentID == 0;
        }

        /// <summary>
        /// Form验证
        /// </summary>
        private bool ValidateForm()
        {
            epLogin.Clear();
            if (this.tbLoginName.Text.Length == 0)
            {
                epLogin.SetError(this.tbLoginName, "用户名不能为空！");
                return false;
            }
            if (this.tbLoginPWD.Text.Length == 0)
            {
                epLogin.SetError(this.tbLoginPWD, "密码不能为空！");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 登录失败，用户名或密码错误
        /// </summary>
        private void LoginFail()
        {
            epLogin.Clear();
            epLogin.SetError(this.tbLoginName, "用户名密码不正确，请重新录入");
            epLogin.SetError(this.tbLoginPWD, "用户名密码不正确，请重新录入");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
